package com.hlc.helloWord.common.redis;

import com.hlc.helloWord.common.exception.MyException;
import com.hlc.helloWord.common.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Created by helinchong on 2017/12/21.
 */
@Service
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    public Object get(String key){
        return redisTemplate.opsForValue().get(key);
    }

    public void set(String key,String value){
        redisTemplate.opsForValue().set(key,value);
    }

    public void set(String key,String value,long seconds){
        redisTemplate.opsForValue().set(key,value,seconds, TimeUnit.SECONDS);
    }

    public void del(String key){
        try{
            redisTemplate.delete(key);
        }catch (Exception e){
            throw new MyException(ResultEnum.ERROR.getCode(),"删除key异常!");
        }

    }

    public void expire(String key,long seconds){
        redisTemplate.expire(key,seconds,TimeUnit.SECONDS);
    }
}
